package controllers;

import helpers.ConsoleUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

import be.objectify.deadbolt.java.actions.Group;
import be.objectify.deadbolt.java.actions.Restrict;

import models.Project;
import models.Resource;

import play.mvc.Controller;
import play.mvc.Result;
import services.ConfigurationService;
import services.TestCaseService;
import services.TestSuiteService;
import views.html.testcases.*;

public class TestCases extends Controller {

	private static final ConfigurationService configurationService = ConfigurationService.getService();
	private static final TestSuiteService testSuiteService = TestSuiteService.getService();
	private static final TestCaseService testCaseService = TestCaseService.getService();

	@Restrict(@Group(Application.ROLE_USER))
	public static Result listTestCases(long projectId, long testSuiteId) {
		List<Resource> testCases = testCaseService.getAllTestCases(testSuiteId);
		return ok(list.render(projectId, testSuiteId, testCases));
	}

	/*
	 * @Restrict(@Group(Application.ROLE_USER)) public static Result addTestCase(long projectId, long testSuiteId) { Form<Resource> form = Form.form(Resource.class); List<Resource> testCases = testCaseService.getAllTestCases(testSuiteId); return ok(add.render(form, projectId, testSuiteId, testCases)); }
	 * 
	 * @Restrict(@Group(Application.ROLE_USER)) public static Result editTestCase(long projectId, long testSuiteId, long testCaseId) { Resource testCase = testCaseService.getTestCase(testCaseId); if (testCase != null) { Form<Resource> form = Form.form(Resource.class).fill(testCase); List<Resource> testCases = testCaseService.getAllTestCases(testSuiteId); return ok(edit.render(testCaseId, form, projectId, testSuiteId, testCases)); } else { return redirect(controllers.routes.TestCases.listTestCases(projectId, testSuiteId)); } }
	 * 
	 * @Restrict(@Group(Application.ROLE_USER)) public static Result createTestCase(long projectId, long testSuiteId) { Resource testCase = Form.form(Resource.class).bindFromRequest().get(); // FilePart testCaseFile = upload(); testCaseService.saveTestCase(projectId, testCase); return redirect(controllers.routes.TestCases.editTestCase(projectId, testSuiteId, testCase.getId())); }
	 * 
	 * @Restrict(@Group(Application.ROLE_USER)) public static Result updateTestCase(long projectId, long testSuiteId, long testCaseId) { Resource testCase = testCaseService.getTestCase(testCaseId); if (testCase != null) { Resource newTestCase = Form.form(Resource.class).bindFromRequest().get(); testCase.setName(newTestCase.getName()); testCase.setDescription(newTestCase.getDescription()); // FilePart testCaseFile = upload(); testCaseService.saveTestCase(projectId, testCase); return redirect(controllers.routes.TestCases.editTestCase(projectId, testSuiteId, testCaseId)); } else { return redirect(controllers.routes.TestCases.listTestCases(projectId, testSuiteId)); } }
	 */

	@Restrict(@Group(Application.ROLE_USER))
	public static Result runTestWithDefaultConfig(long projectId, long testSuiteId, long testCaseId) {
		Resource config = Project.findById(projectId).getDefaultConfiguration();
		return runTestWithConfig(projectId, testSuiteId, testCaseId, config);
	}

	@Restrict(@Group(Application.ROLE_USER))
	public static Result runTestWithConfig(long projectId, long testSuiteId, long testCaseId, long configId) {
		Resource config = configurationService.getConfig(configId);
		return runTestWithConfig(projectId, testSuiteId, testCaseId, config);
	}

	private static Result runTestWithConfig(long projectId, long testSuiteId, long testCaseId, Resource config) {
		Project project = Project.findById(projectId);
		Resource testSuite = testSuiteService.getTestSuite(testSuiteId);
		Resource testCase = testCaseService.getTestCase(testCaseId);
		if (project == null || testSuite == null || testCase == null || config == null) {
			return redirect(controllers.routes.TestCases.listTestCases(projectId, testSuiteId));
		}
		String content = "";
		try {
			String testSuiteName = testSuite.getName();
			if (ConsoleUtils.hasFileExtension(testSuiteName, TestSuites.TEST_SUITE_FILE_EXTENSIONS)) {
				testSuiteName = testSuiteName.substring(0, testSuiteName.lastIndexOf('.'));
			}
			String testCaseName = testCase.getName().replace(" ", "");
			URL url = testCaseService.getTestExecuteURL(project, testSuiteName, testCaseName, config);

			URLConnection connection = url.openConnection();
			BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
			String line;
			while ((line = in.readLine()) != null) {
				content += line + "\n";
			}
			in.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ok(content);
	}

}
